package com.am.file.controller;


import com.am.api.dto.MeetingFileDto;
import com.am.common.domain.Result;
import com.am.file.config.MinIOConfig;
import com.am.file.service.impl.MeetingFilesServiceImpl;
import com.am.file.utils.MinIOUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/file")
public class MeetingFilesController {

    @Resource
    MinIOConfig minIOConfig;

    @Autowired
    MeetingFilesServiceImpl meetingFilesService;

    @PostMapping("/uploadfiles")
    protected Result uploadfiles(@RequestParam("files") MultipartFile[] files){
        System.out.println("======================");
        try {
            List<String> fileUrls = MinIOUtils.uploadFiles(minIOConfig.getBucketName(), files);
            if(fileUrls.size()>0) {
                return Result.success(fileUrls);
            }else {
                return Result.error("上传失败");
            }

        }catch (Exception e){
            return Result.error("文件异常");
        }
    }


    @PostMapping("/upload")
    protected Result upload(@RequestParam("file") MultipartFile file) {
        System.out.println("======================");
        try {
            String s = MinIOUtils.uploadFile(minIOConfig.getBucketName(), file);
            return Result.success(s);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }

    @GetMapping("/get")
    protected Result get() throws Exception {
        System.out.println("==============");
        return Result.success();
    }

    @DeleteMapping("/delete")
    protected Result delete(@RequestBody List<String> files) throws Exception {
        System.out.println(files);
        if(MinIOUtils.deleteFiles(files))
        {
            return Result.success();
        }else
        {
            return Result.error("删除失败");
        }

    }
    @PostMapping("/save")
    Collection<Long> saveFile(@RequestBody List<MeetingFileDto> meetingFileDtos){

       return meetingFilesService.saveFile(meetingFileDtos);
    }
    @RequestMapping("/test")
      String test(){
        System.out.println("==============");
        return "test";
      }
}


